package com.lsp.lib_uikit.commonrecycler;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

/**
 * Created by xcj on 17/8/2.
 */

public class BaseRecyclerView extends RecyclerView {

  private LinearLayoutManager mLinearManager;
  private boolean isGettingTop;

  public BaseRecyclerView(Context context) {
    super(context);
    init();
  }

  public BaseRecyclerView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public BaseRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }

  private void init() {
    addOnScrollListener(new OnScrollListener() {
      //int firstVisiblePosition;
      int firstCompleteVisibleItem;

      @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if (mLinearManager != null) {
          firstCompleteVisibleItem = mLinearManager.findFirstCompletelyVisibleItemPosition();
          isGettingTop = firstCompleteVisibleItem == 0;
        }
      }
    });
  }

  public boolean isGettingTop() {
    return isGettingTop;
  }

  @Override public void setLayoutManager(LayoutManager layout) {
    super.setLayoutManager(layout);

    if (layout instanceof LinearLayoutManager) {
      mLinearManager = (LinearLayoutManager) layout;
    }
  }
}
